package com.lucher.mes.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.DepotrackMapper;
import com.lucher.mes.dao.DepotrackinfoMapper;
import com.lucher.mes.po.Depotrack;
import com.lucher.mes.po.Depotrackinfo;
import com.lucher.mes.po.User;
import com.lucher.mes.service.IDepotrackService;

@Controller
@RequestMapping("/depotrack")  
public class DepotrackController extends BaseController{
	@Resource  
    private IDepotrackService depotrackService; 
	@Autowired
	private DepotrackMapper depotrackDao;
	@Autowired
	private DepotrackinfoMapper depotrackinfoDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Depotrack form) {
		return "depotrack/findDepotrackList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Depotrack form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = depotrackService.findByCount(form);
		List<Depotrack> list = depotrackService.findByListForPage(form);
		
		for (Depotrack depotrack : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", depotrack.getId());
			objectMap.put("depotrackcode", depotrack.getDepotrackcode());
			objectMap.put("depotrackname", depotrack.getDepotrackname());
			objectMap.put("rownums", depotrack.getRownums());
			objectMap.put("columnnums", depotrack.getColumnnums());
			objectMap.put("racknum", depotrack.getRacknum());
			objectMap.put("remark", depotrack.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Depotrack form) {
		return "depotrack/addDepotrack";
	}

	/**
	 * 新增
	 * @throws Exception 
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	@Transactional //启动事务
	public Map<String,Object>  save(Depotrack form){
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getDepotrackcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入货柜编码");
			return responseJson;
		} else {
			Depotrack oldDepotrack = depotrackService.findByDepotrackcode(form.getDepotrackcode());
			if (oldDepotrack != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此货柜编码已经存在");
				return responseJson;
			}
		}

		//货架数量后台重新计算 不能使用前端提交的数据
		form.setRacknum(form.getColumnnums() * form.getRownums()  );
		//保存货柜信息
		Integer depotrackId = depotrackDao.insertSelective(form);
		
		//保存成功
		if(depotrackId != 0){
			ArrayList<Depotrackinfo> depotrackinfoList = new ArrayList<Depotrackinfo>();
			ArrayList<String> strList = form.getStrList();
			for (int i = 1; i < strList.size(); i++) {
				Depotrackinfo depotrackinfo = new Depotrackinfo();
				depotrackinfo.setDepotrackcode(form.getDepotrackcode());
				depotrackinfo.setDepotrackname(form.getDepotrackname());
				depotrackinfo.setRownum(Integer.parseInt(strList.get(i).split("-")[1]));
				depotrackinfo.setColumnnum(Integer.parseInt(strList.get(i).split("-")[2]));
				depotrackinfo.setCodeinfo(strList.get(i));
				depotrackinfoList.add(depotrackinfo);
				//depotrackinfoDao.insertSelective(depotrackinfo);
			}
			depotrackinfoDao.saveBatch(depotrackinfoList);
		}
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Depotrack form,Depotrackinfo record) {
		Depotrack depotrack = depotrackService.selectByPrimaryKey(form.getId());
		form.setDepotrack(depotrack);

		record.setDepotrackcode(depotrack.getDepotrackcode());
		List<Depotrackinfo> depotrackinfoList = depotrackinfoDao.findByList(record);
		form.setDepotrackinfolist(depotrackinfoList);
		
		return "depotrack/updateDepotrack";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Depotrack form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getDepotrackcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，货柜编码不能为空");
			return responseJson;
		} else {
			Depotrack oldDepotrack = depotrackService.findByDepotrackcode(form.getDepotrackcode());
			if (oldDepotrack != null && !oldDepotrack.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，货柜编码已经存在");
				return responseJson;
			}
		}
		 //重算仓位数量
		 form.setRacknum(form.getColumnnums() * form.getRownums()  );

		// 修改信息
		int flag = depotrackService.updateByPrimaryKeySelective(form);
		
		if(flag != 0){//修改成功
			//删除旧的仓位
			depotrackinfoDao.deleteByDepotrackcode(form.getDepotrackcode());
			//依次增加新的仓位
			ArrayList<Depotrackinfo> depotrackinfoList = new ArrayList<Depotrackinfo>();
			List<String> strList = form.getStrList();
			for (int i = 1; i < strList.size(); i++) {
				Depotrackinfo depotrackinfo = new Depotrackinfo();
				depotrackinfo.setDepotrackcode(form.getDepotrackcode());
				depotrackinfo.setDepotrackname(form.getDepotrackname());
				depotrackinfo.setRownum(Integer.parseInt(strList.get(i).split("-")[1]));
				depotrackinfo.setColumnnum(Integer.parseInt(strList.get(i).split("-")[2]));
				depotrackinfo.setCodeinfo(strList.get(i));
				depotrackinfoList.add(depotrackinfo);
				//depotrackinfoDao.insertSelective(depotrackinfo);
			}
			depotrackinfoDao.saveBatch(depotrackinfoList);
		}
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Depotrack form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		// 删除网络，实际上就是修改state为0-无效
		Depotrack depotrackdmp = depotrackService.selectByPrimaryKey(form.getId());
		
		//删除货柜
		depotrackDao.deleteByPrimaryKey(form.getId());
				
		//删除旧的仓位
		depotrackinfoDao.deleteByDepotrackcode(depotrackdmp.getDepotrackcode());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 删除操作员
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Depotrack> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Depotrack depotrack : formlist) {
			//删除货柜
			depotrackDao.deleteByPrimaryKey(depotrack.getId());
			//删除旧的仓位
			depotrackinfoDao.deleteByDepotrackcode(depotrack.getDepotrackcode());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
